<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.net.URLEncoder" %>
<jsp:useBean id="loginUser" class="mo.org.cm309.shoppingcart.entity.User" scope="session"></jsp:useBean>
<jsp:useBean id="product" class="mo.org.cm309.shoppingcart.entity.Product" scope="request"></jsp:useBean>
<% 
	String retUrl = "";
	StringBuffer sUrl = new StringBuffer("");
	if (request != null){
		if (request.getAttribute("javax.servlet.forward.servlet_path") != null) {
		//if (request.getHeader("Referer") != null){
			 sUrl.append(URLEncoder.encode((String)request.getAttribute("javax.servlet.forward.servlet_path"), "UTF-8"));
		//	sUrl.append(URLEncoder.encode(request.getHeader("Referer"), "UTF-8"));
		//	sUrl.append(URLEncoder.encode(request.getPathInfo(), "UTF-8"));
		}else{
			 sUrl.append(URLEncoder.encode((String)request.getServletPath(), "UTF-8"));
		}
		if (request.getQueryString() != null) {
			 sUrl.append("?");
			 sUrl.append(URLEncoder.encode(request.getQueryString(), "UTF-8"));
		}
		if (sUrl.toString() != null && !sUrl.toString().equals(""))
			retUrl = (URLDecoder.decode(sUrl.toString(), "UTF-8")).substring(1);
	}
%>

<div id="header">
	<div id="logo">
		<img src="images/logo.png">
	</div>
	<form>
		<div id="searchbar">
			<table border="0">
				<tr>
					<td><input id="keyword" title="Search for"></td>
					<td><input type="submit" id="searchicon" value="Go" /></td>
					<td align="right">
						<% if (loginUser 		   != null &&
							   loginUser.getEmail()!= null) {%>
							<label style="display: block; width: 250px;">Welcome! <%=loginUser.getLastname() + " " + loginUser.getFirstname() %></label>
						<%} %>
					</td>
				</tr>
			</table>
		</div>
	</form>
	<div id="menu">
		<form style="margin-top: 1em;">
			<div id="radioset">
				<input type="radio" id="radio1" name="radio"><label for="radio1"><a href="home.jsp">Home</a></label>
				<% if (loginUser 		   != null &&
					   loginUser.getRoleId() == 1) {%>
					<input type="radio" id="radio2" name="radio"><label for="radio2"><a href="productAdmin.jsp">Product</a></label>
				<%} else if (loginUser 		   != null&&
					   loginUser.getRoleId() == 2) {%>
					<input type="radio" id="radio2" name="radio"><label for="radio2"><a href="productDetailServlet?productId=<%=product.getId()%>">Product</a></label>
				<%} else {%>
					<input type="radio" id="radio2" name="radio"><label for="radio2"><a href="login.jsp?requestURI=<%=retUrl%>">Product</a></label>
				<%} %>
				<input type="radio" id="radio4" name="radio"><label for="radio4"><a href="showCartServlet">My Cart</a></label>
				<input type="radio" id="radio5" name="radio"><label for="radio5"><a href="myorder.jsp">My Order</a></label>
				<% if (loginUser 		   != null &&
					   loginUser.getEmail()!= null) {%>
					<input type="radio" id="radio6" name="radio"><label for="radio6"><a href="logoutServlet?requestURI=<%=retUrl%>">Logout</a></label>
				<%} else { %>
					<input type="radio" id="radio6" name="radio"><label for="radio6"><a href="login.jsp?requestURI=<%=retUrl%>">Login</a></label>
				<%} %>
			</div>
		</form>
	</div>
</div>
